<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['id']){
		$params['zhaoshangid']=intval($_RQ['id']);
		$return['params']['id']=$_RQ['id'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$fields='';
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_mzhaoshang_merch',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$mfields=array('id','salecate','mobile','realname','status','jointime','accounttime','merchname');
	foreach($return['list'] as &$v){
		$v['merch']=pdo_get('jy_merch_user',array('id'=>$v['merchid'],'uniacid'=>$_SESSION['uniacid']),$mfields);
		$v['jointime']=date('Y/m/d H:i:s',$v['merch']['jointime']);
		$v['url']=custom_url('jy_merch/merch//id='.$v['merchid'],true);
		$v['accounttime']=$v['merch']['accounttime']?date('Y/m/d H:i:s',$v['merch']['accounttime']):'无';
		$v['status']=$v['merch']['status'];
	}
	$return['status']=array('0'=>array('id'=>0,'title'=>'待入驻'),'1'=>array('id'=>1,'title'=>'入驻中'),'2'=>array('id'=>2,'title'=>'暂停中'));
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'多商户管理'),
			//array('op'=>'edit','title'=>'添加多商户'),
		),
		'list'=>array('data'=>'list','del_one'=>false,'delall'=>false,'edit'=>false,'list'=>array(
			array('name'=>'merch.merchname','title'=>'商户名称','type'=>'string'),
			array('name'=>'merch.salecate','title'=>'主营项目','type'=>'string'),								
			array('name'=>'merch.realname','title'=>'联系人','type'=>'string'),
			array('name'=>'merch.mobile','title'=>'电话','type'=>'string'),					
			array('name'=>'jointime','title'=>'入驻时间','type'=>'string'),
			array('name'=>'accounttime','title'=>'到期时间','type'=>'string'),								
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status2'),	
			array('name'=>'url','title'=>'入口','type'=>'ewm'),						
		),
		)
	);
	$return['status2']=array('1'=>'入驻中','2'=>'暂停中');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$merchid=pdo_getcolumn('jy_mzhaoshang_merch',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),'merchid');
		$item=pdo_get('jy_merch_user',array('id'=>$merchid,'uniacid'=>$_SESSION['uniacid']));
		$item['lat_lng']=json_decode($item['lat_lng']);
		$item['onlinetime']=json_decode($item['onlinetime'],true);
		$item['oldstatus']=$item['status'];
	}else{
		$item=array();
	}
	$item['codeimg']=$item['codeimg']?json_decode($item['codeimg'],true):array();
	$item['pcd']=$item['pcd']?json_decode($item['pcd'],true):array('province'=>'');
	if(!$item['pcd']['province']){
		$item['pcd']=array('province'=>'');
	}
	$choufee_set=$item['choufee_set']=$item['choufee_set']?json_decode($item['choufee_set'],true):array('commission_type'=>'0','shopcommission_type'=>'0');
	if(!is_array($choufee_set)){
		$choufee_set=array('commission_type'=>'0','shopcommission_type'=>'0');
	}
	//$item['datamids']=json_decode($item['datamids'],true);
	$choufee_set['rate']=$choufee_set['rate']?:0;	
	$item=array_merge($item,$choufee_set);
	$item['upass']='';
	if($item['onewechat']){
		$item['onewechat']=json_decode($item['onewechat'],true);
		if(!$item['onewechat']['token']){
			$item['onewechat']=array('isopen'=>0);
		}
	}else{
		$item['onewechat']=array('isopen'=>0);
	}
	$item['app']=$item['app']?json_decode($item['app'],true):array('isopen'=>0);
	$item['installbak']=$item['app']['installbak'];
	$item['appurl']=custom_url('jy_merch/getapp//merchid='.$item['id'],true);
	$item['url']=SITEROOT.'addons/jy_merch/wxapi.php?merchid='.$item['id'];
	$item['onewechat']['token']=$item['onewechat']['token']?:random(30);
	$item['onewechat']['aeskey']=$item['onewechat']['aeskey']?:random(43);
	$item['cert_file']=$item['onewechat']['cert_file'];
	$item['key_file']=$item['onewechat']['key_file'];
	$account_host=get_account_url();
	$account_tp=str_replace('http://','',$account_host).'custom/';
	$account_apitp=str_replace('http://','',$account_host).'app/';
	$account_url=str_replace('http://','',$account_host).'custom/payresult/wechat_notify.php';
    $return['item']=$item;
	$htmls=array( 
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'多商户设置',
		'tabs'=>array(
			array('op'=>'list','title'=>'多商户管理'),
			array('op'=>'edit','title'=>'添加多商户'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本设置',
				'list'=>array(
				array('name'=>'type','title'=>'商户类型','type'=>'radio','options'=>'merch_type'),
				array('name'=>'merchname','must'=>true,'title'=>'商户名称','type'=>'string'),
				array('name'=>'logo','title'=>'商户logo','type'=>'image'),
				array('type'=>'image','title'=>'店招','name'=>'topimg'),
				array('name'=>'isbrand','title'=>'属于品牌？','type'=>'switch'),
				array('name'=>'star','title'=>'评星','type'=>'unitstring','unit'=>'星'),
				array('name'=>'sudu','title'=>'速度评星','type'=>'unitstring','unit'=>'星'),
				array('name'=>'salecate','title'=>'主营项目','type'=>'string'),	
				array('name'=>'cateid','must'=>true,'title'=>'商户分类','type'=>'select','options'=>'cate','option_title'=>'catename'),
				array('name'=>'realname','title'=>'联系人','type'=>'string'),
				array('name'=>'mobile','title'=>'电话','type'=>'string'),
				array('name'=>'offline','title'=>'休息?','type'=>'switch','show'=>array('type'=>1)),
				array('type'=>'time_hour','title'=>'营业时间','name'=>'onlinetime','show'=>array('type'=>1)),							
				//array('name'=>'datamids','title'=>'选择操作员','type'=>'choose_many','action'=>'core/member.get_member/many','help'=>'只有查看商户数据的权限！'),	
				array('name'=>'desc','title'=>'商户简介','type'=>'text'),
				array('type'=>'text','title'=>'商户公告','name'=>'remark'),
				array('name'=>'pcd','title'=>'商户区域','type'=>'pcd'),
				array('name'=>'address','title'=>'商户地址','type'=>'string'),	
				array('name'=>'lat_lng','must'=>true,'title'=>'坐标','type'=>'lat_lng'),
				array('name'=>'tel','title'=>'商户电话','type'=>'string'),
				
				array('name'=>'isrecommand','title'=>'是否推荐','type'=>'radio','options'=>'status2'),	
				array('name'=>'salenum','title'=>'销量','type'=>'string'),
				array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
				array('name'=>'groupid','title'=>'商户组','type'=>'select','options'=>'group','option_title'=>'groupname'),	
				array('name'=>'codeimg','title'=>'营业执照/许可证','type'=>'images'),
				array('name'=>'content','title'=>'商户详情','type'=>'richtext'),
				)
				),
				array(
					'title'=>'店长信息',
					'list'=>array(
						array('name'=>'uname','must'=>true,'title'=>'账户名','type'=>'string'),
						array('name'=>'upass','title'=>'账户密码','type'=>'string'),
						array('name'=>'mid','title'=>'选择管理员','type'=>'choose','action'=>'core/member.get_member/'),	
						array('name'=>'payopenid','title'=>'选择收款人','type'=>'choose_title','action'=>'core/member.get_member/'),						
					)
				),
				array(
					'title'=>'抽成设置',
					'list'=>array(
						//array('name'=>'payrate','title'=>'抽成利率（%）','type'=>'string'),
						array('type'=>'unitstring','name'=>'cong_choufee','title'=>'会员充值抽成','unit'=>'%','help'=>'会员充值，平台抽成充值金额的N%，不填表示不支持，自己发展的会员充值不提成！'),
						array('type'=>'radio','title'=>'外卖单佣金计算方式','name'=>'commission_type','options'=>'commission_types','help'=>'【<red>温馨提示</red>】：基本设置--外卖单佣金计算方式--设置为《独立百分比抽成》才生效！','show'=>array('type'=>1)),
						array('type'=>'checkbox','title'=>'抽成项目','name'=>'dish_feetype','options'=>'dish_feetypes'),
						array('type'=>'checkbox','title'=>'不抽成项目','name'=>'no_feetype','options'=>'no_feetypes'),
						array('type'=>'unitstring','name'=>'rate','unit'=>'%','title'=>'抽成比例','show'=>array('commission_type'=>0),'help'=>'按百分比算：例如5，表示收起5%'),
						array('type'=>'unitstring','name'=>'rateyuan','unit'=>'元','title'=>'固定抽成','show'=>array('commission_type'=>1),'help'=>'按每单固定金额抽成！'),
						array(
										'type'=>'array',
										'name'=>'ratejieti',
										'title'=>'阶梯抽成',
										'show'=>array('commission_type'=>2),
										'add_tip'=>'添加一个阶梯',
											'list'=>array(
												array('type'=>'string','name'=>'fee','title'=>'订单满多少'),
												array('type'=>'string','name'=>'rate','title'=>'抽成%'),
											),
											'details'=>array(
												array('type'=>'unitstring','unit'=>'元','name'=>'fee','title'=>'订单满多少'),
												array('type'=>'unitstring','unit'=>'%','name'=>'rate','title'=>'抽成%'),
											)
						),
						array('type'=>'radio','title'=>'商城单佣金计算方式','name'=>'shopcommission_type','options'=>'commission_types','help'=>'【<red>温馨提示</red>】：基本设置--商城单佣金计算方式--设置为《独立百分比抽成》才生效！','show'=>array('type'=>0)),
						array('type'=>'checkbox','title'=>'抽成项目','name'=>'shop_feetype','options'=>'shop_feetypes'),
						array('type'=>'checkbox','title'=>'不抽成项目','name'=>'shopno_feetype','options'=>'no_feetypes'),
						array('type'=>'unitstring','name'=>'shoprate','unit'=>'%','title'=>'抽成比例','show'=>array('shopcommission_type'=>0),'help'=>'按百分比算：例如5，表示收起5%'),
						array('type'=>'unitstring','name'=>'shoprateyuan','unit'=>'元','title'=>'固定抽成','show'=>array('shopcommission_type'=>1),'help'=>'按每单固定金额抽成！'),
						array(
										'type'=>'array',
										'name'=>'shopratejieti',
										'title'=>'阶梯抽成',
										'show'=>array('shopcommission_type'=>2),
										'add_tip'=>'添加一个阶梯',
											'list'=>array(
												array('type'=>'string','name'=>'fee','title'=>'订单满多少'),
												array('type'=>'string','name'=>'rate','title'=>'抽成%'),
											),
											'details'=>array(
												array('type'=>'unitstring','unit'=>'元','name'=>'fee','title'=>'订单满多少'),
												array('type'=>'unitstring','unit'=>'%','name'=>'rate','title'=>'抽成%'),
											)
						),	
					)
			),
			array(
				'title'=>'配送设置',
				'list'=>array(
					array('name'=>'sendtype','title'=>'默认配送方式','type'=>'radio','options'=>'sendtypes'),
					array('name'=>'mypeisong','title'=>'是否自己配送','type'=>'switch','help'=>'不开启，则由平台配送员统一配送！','show'=>array('type'=>1)),
					array('name'=>'out_pay','title'=>'配送费','type'=>'string','show'=>array('type'=>1)),
					array('name'=>'out_price','title'=>'起送金额','type'=>'string','show'=>array('type'=>1)),
					array('name'=>'man_fee','title'=>'满额免配送费','type'=>'string','show'=>array('type'=>1)),
					array('name'=>'out_juli','title'=>'最大配送距离','type'=>'unitstring','unit'=>'KM','help'=>'留空则由平台统一','show'=>array('type'=>1)),
					array('name'=>'out_upfee','title'=>'打包费','type'=>'unitstring','unit'=>'元','help'=>'留空则为0！','show'=>array('type'=>1)),	
				)
			),
			array(
				'title'=>'服务设置',
				'list'=>array(
					array('name'=>'fuwu_type','title'=>'启用功能','type'=>'checkbox','options'=>'fuwu_types'),
					array('name'=>'yuyue_type','title'=>'预约功能','type'=>'radio','options'=>'yuyue_types'),
				)
			),
		)
	);
	if($this->setting['isone_wechat']){
		$htmls['edits'][]=array(
				'title'=>'独立微信支付',
				'list'=>array(
					array('type'=>'tip','title'=>'【<red>温馨提示</red>】：
					</br>1、一定要在微信支付商户后台配送好微信支付的回调域名，否则不能正常支付回调！
					<br>2、JSAPI支付授权目录：'.$account_tp.'，兼容插件支付授权目录：'.$account_apitp.'
					<br>3、扫码回调链接：'.$account_url.'
					<br>4、微信支付商户的密钥不对，也可能导致支付回调异常（支付成功后：可能订单状态还是未支付）。'
					),
					array('type'=>'switch','title'=>'开启独立微信支付？','name'=>'onewechat.isopen','help'=>'开启后，商户可以设置自己的微信公众号，微信支付商户等！'),	
					array('type'=>'radio','title'=>'公众号类型','name'=>'onewechat.type', 'options'=>'types','help'=>'请设置正确的公众号类型，以免出错！(非认证服务号，可以借用系统认证服务号权限，否则无法微信一键登录，获取粉丝信息等；如果是认证服务号，需移步《微信公众号平台》--》接口设置--》网页授权回调域名，设置好域名才能正常使用！)'),
					array('type'=>'string','title'=>'AppID','name'=>'onewechat.appid','help'=>'填写公众号开发者AppID。登陆微信公众平台——开发——基本配置——开发者ID'),
					array('type'=>'string','title'=>'AppSecre','name'=>'onewechat.appsecret','help'=>'填写公众号开发者AppSecret。登陆微信公众平台——开发——基本配置——开发者密钥'),
					array('type'=>'string','title'=>'原始ID','name'=>'onewechat.oldid','help'=>'填写公众号原始ID。登陆微信公众平台——设置——公众号设置——帐号详情——原始ID'),
					array('type'=>'string','title'=>'微信支付商户号','name'=>'onewechat.wechat_mchid'),
					array('type'=>'string','title'=>'商户支付密钥','name'=>'onewechat.wechat_miyue','help'=>'如果是服务商支付，此处填写服务商支付密钥'),
					array('type'=>'upfile','title'=>'CRET证书文件'.$cert_remark,'name'=>'cert_file','help'=>'下载证书 cert.zip 解压后，上传 apiclient-cert.pem 文件。'),
					array('type'=>'upfile','title'=>'KEY密钥文件'.$key_remark,'name'=>'key_file','help'=>'下载证书 cert.zip  解压后，上传 apiclient-key.pem 文件。'),	
					array('type'=>'tip','title'=>'公众号绑定方法（授权模式添加公众号请忽略此步骤）：根据提示填写上述参数之后，将以下三项参数填至微信公众平台——开发——基本配置——服务器配置，启用，提交即可。'),
					array('type'=>'tip','title'=>'<red>[token验证失败?]:如果是https的：微信目前HTTPS他是有安全门槛的，达不到ATS安全标准证书或SSL证书安装不规范，就算安装了HTTPS也是不会通过微信认证；如果是其他原因，可以百度搜索一下自行排查。</red>' ),
					array('type'=>'copy','title'=>'服务器地址','name'=>'url', 'help'=>'' ),
					array('type'=>'copy','title'=>'令牌（Token）','name'=>'onewechat.token','help'=>'' ),
					array('type'=>'copy','title'=>'消息加解密密钥(EncodingAESKey)','name'=>'onewechat.aeskey','help'=>''),
				)
		);
	}
	if($this->setting['isone_app']){
		$htmls['edits'][]=array(
				'title'=>'下载APP',
				'list'=>array(
					array('type'=>'switch','title'=>'支持下载APP？','name'=>'app.isopen'),	
					array('type'=>'copy','title'=>'APP下载地址','name'=>'appurl','help'=>''),
					array('type'=>'ewm','title'=>'APP下载二维码','name'=>'appurl','help'=>''),
					array('type'=>'upfile','title'=>'上传APP安装包','name'=>'installbak'),
				)
		);
	}
	$citypeisong=plugin('jy_weishop')->plugin_setting('citypeisong');
	$return['fuwu_types']=$this->fuwu_types;
	$return['yuyue_types']=$this->yuyue_types;
	if($citypeisong['isopen']){
		$return['sendtypes']=array(
				'1'=>'同城配送',
				'2'=>'到店自取',
		);
	}else{
		$return['sendtypes']=array(
			'1'=>'快递配送',
			'2'=>'到店自取',
		);
	}
	$return['cate']=pdo_getall('jy_merch_category',array('uniacid'=>$_SESSION['uniacid']),array('id','catename'));
	$return['group']=pdo_getall('jy_merch_group',array('uniacid'=>$_SESSION['uniacid']),array('id','groupname'));
	$return['status']=$this->merch_status;
	$return['status2']=array('1'=>'是','0'=>'否');
	$return['commission_types']=$this->commission_types;
	$return['merch_type']=$this->merch_type;
	unset($return['commission_types'][3]);
	$return['dish_feetypes']=$this->dish_feetypes;
	$return['shop_feetypes']=$this->shop_feetypes;
	$return['no_feetypes']=$this->no_feetypes;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	if(!$post['merchname'] ||  !$post['uname']){
		exi('商户名称和账户必填','error');
	}
	if(!$post['cateid']){
		exi('商户分类必选！','error');
	}
	if(!$post['lat_lng']){
		exi('请填写坐标！','error');
	}
	if($post['star'] >5){
		exi('评星不能大于5！','error');
	}
	if($post['sudu'] >5){
		exi('速度评星不能大于5！','error');
	}
	if($post['oldstatus']==1 && $post['status']!=1 && $post['id']){
		pdo_update('jy_weishop_goods',array('nochecked'=>1),array('merchid'=>$post['id'],'uniacid'=>$_SESSION['uniacid']));
	}
	$infomid=pdo_getcolumn('jy_merch_user',array('mid'=>$post['mid'],'uniacid'=>$_SESSION['uniacid']),'mid');
	$uname=pdo_get('jy_merch_user',array('uname'=>$post['uname']));
	$post['choufee_set']=array(
		'cong_choufee'=>$post['cong_choufee'],
		'commission_type'=>$post['commission_type'],
		'dish_feetype'=>$post['dish_feetype'],
		'no_feetype'=>$post['no_feetype'],
		'rate'=>$post['rate'],
		'rateyuan'=>$post['rateyuan'],
		'ratejieti'=>$post['ratejieti'],
		'shopcommission_type'=>$post['shopcommission_type'],
		'shop_feetype'=>$post['shop_feetype'],
		'shopno_feetype'=>$post['shopno_feetype'],
		'shoprate'=>$post['shoprate'],
		'shoprateyuan'=>$post['shoprateyuan'],
		'shopratejieti'=>$post['shopratejieti'],
	);
	$post['app']['installbak']=$post['installbak'];
	$post['onewechat']['cert_file']=$post['cert_file'];
	$post['onewechat']['key_file']=$post['key_file'];
	$data =array(
		'type'=>intval($post['type']),
		'uname'=>$post['uname'],
		'upass'=>base64_encode($post['upass']),
		'mobile'=>$post['mobile'],
		'realname'=>$post['realname'],
		'merchname'=>$post['merchname'],
		'offline'=>intval($post['offline']),
		'onlinetime'=>ijson_encode($post['onlinetime']),
		'logo'=>$post['logo'],
		'topimg'=>$post['topimg'],
		'desc'=>$post['desc'],
		'remark'=>$post['remark'],
		'salecate'=>$post['salecate'],
		'status'=>$post['status'],
		'address'=>$post['address'],
		'lat_lng'=>ijson_encode($post['lat_lng']),
		'mid'=>$post['mid'],
		'cateid'=>intval($post['cateid']),
		'star'=>intval($post['star']),
		'sudu'=>intval($post['sudu']),
		'isrecommand'=>$post['isrecommand'],
		'tel'=>$post['tel'],
		'out_pay'=>$post['out_pay'],
		'out_price'=>$post['out_price'],
		'man_fee'=>$post['man_fee'],
		'salenum'=>$post['salenum'],
		'groupid'=>$post['groupid'],
		'payrate'=>0,
		'payopenid'=>$post['payopenid'],
		'isbrand'=>$post['isbrand'],
		'content'=>trim($post['content']),
		'mypeisong'=>$post['mypeisong'],
		'out_juli'=>$post['out_juli'],
		'out_upfee'=>$post['out_upfee'],
		'choufee_set'=>ijson_encode($post['choufee_set']),
		'codeimg'=>ijson_encode($post['codeimg']),
		'pcd'=>ijson_encode($post['pcd']),
		'sendtype'=>intval($post['sendtype']),
		'fuwu_type'=>trim($post['fuwu_type']),
		'yuyue_type'=>trim($post['yuyue_type']),
		'onewechat'=>ijson_encode($post['onewechat']),
		'app'=>ijson_encode($post['app']),
	);
	if($post['id']){
		$_SESSION['isopenid'.$post['id']]=0;
		if(!$post['upass']){
			unset($data['upass']);
		}
		if($infomid && $infomid!=$post['mid']){
			//exi('一个管理员只能绑定一个账号','error');
		}
		if($uname && $uname['id']!=$post['id']){
			exi('登陆账户名不可以重复','error');	
		}
		pdo_update('jy_merch_user',$data,array('id'=>$post['id']));
		exi('保存成功','','list');
	}else{
		if($uname){
			exi('登陆账户名不可以重复','error');	
		}
		if($infomid){
			//exi('一个管理员只能绑定一个账号','error');
		}
		$data['uniacid']=$_SESSION['uniacid'];
		$data['jointime']=time();
		pdo_insert('jy_merch_user',$data);
		exi('添加成功','','list');
	}	
}
if($_RQ['op']=='del'){

	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_mzhaoshang_merch',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_mzhaoshang_merch',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));	
			}
		}
	}
	exi('删除成功！');
}
?>